<!DOCTYPE html>
<html lang="zh-CN">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>留言板</title>
  <style>
    * {
      margin: 0;
      padding: 0;
      box-sizing: border-box;
      font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    }

    body {
      background-color: #f5f7fa;
      padding: 20px;
    }

    .container {
      max-width: 600px;
      margin: 50px auto;
    }

    .box {
      background-color: white;
      padding: 25px;
      border-radius: 12px;
      box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
      margin-bottom: 30px;
    }

    .input-group {
      display: flex;
      gap: 12px;
      align-items: flex-start;
    }

    #tx {
      flex: 1;
      padding: 12px 15px;
      border: 1px solid #e1e5eb;
      border-radius: 8px;
      font-size: 16px;
      resize: none;
      transition: all 0.3s ease;
      line-height: 1.5;
    }

    #tx:focus {
      outline: none;
      border-color: #55b8e0;
      box-shadow: 0 0 0 3px rgba(85, 184, 224, 0.1);
      height: 100px;
    }

    #tx::placeholder {
      color: #a0aec0;
    }

    .word-count {
      text-align: right;
      margin-top: 8px;
      font-size: 13px;
      color: #a0aec0;
      display: none;
    }

    button {
      padding: 0 18px;
      background-color: #55b8e0;
      color: white;
      border: none;
      border-radius: 8px;
      font-size: 16px;
      cursor: pointer;
      transition: all 0.3s ease;
      height: 46px;
    }

    button:hover {
      background-color: #4aa5cc;
      transform: translateY(-1px);
    }

    button:active {
      transform: translateY(1px);
    }

    #tx:focus+button {
      height: 100px;
    }

    .message-list {
      display: flex;
      flex-direction: column;
      gap: 15px;
    }

    .message-card {
      background-color: white;
      padding: 20px;
      border-radius: 12px;
      box-shadow: 0 2px 8px rgba(0, 0, 0, 0.04);
      transition: transform 0.2s ease;
    }

    .message-card:hover {
      transform: translateY(-2px);
    }

    .author {
      font-size: 16px;
      color: #eb8193;
      margin-bottom: 8px;
      font-weight: 500;
    }

    .content {
      font-size: 15px;
      color: #2d3748;
      line-height: 1.6;
      margin-bottom: 12px;
    }

    .time {
      font-size: 13px;
      color: #a0aec0;
    }

    .empty-hint {
      color: #a0aec0;
      text-align: center;
      padding: 30px;
      background-color: white;
      border-radius: 12px;
      box-shadow: 0 2px 8px rgba(0, 0, 0, 0.04);
    }
  </style>
</head>

<body>
  <div class="container">
    <div class="box">
      <div class="input-group">
        <textarea placeholder="请输入您的留言..." id="tx"></textarea>
        <button>发布</button>
      </div>
      <p class="word-count">0/200字</p>
    </div>

    <div class="message-list">
      <div class="empty-hint">暂无留言，快来发布第一条吧~</div>
    </div>
  </div>

  <script>
    const textarea = document.querySelector('#tx')
    const wordCount = document.querySelector('.word-count')
    const button = document.querySelector('button')
    const messageList = document.querySelector('.message-list')
    const emptyHint = document.querySelector('.empty-hint')

    // 显示字数统计
    textarea.addEventListener('focus', function () {
      wordCount.style.display = 'block'
    })

    // 隐藏字数统计
    textarea.addEventListener('blur', function () {
      if (!textarea.value.trim()) {
        wordCount.style.display = 'none'
      }
    })

    // 更新字数统计
    textarea.addEventListener('input', function () {
      const count = textarea.value.length
      wordCount.textContent = `${count}/200字`

      // 超过字数限制时提示
      if (count > 200) {
        wordCount.style.color = '#e53e3e'
        textarea.value = textarea.value.substring(0, 200)
        wordCount.textContent = '200/200字'
      } else {
        wordCount.style.color = '#a0aec0'
      }
    })

    // 发布留言
    function publishMessage() {
      const content = textarea.value.trim()
      if (!content) return

      // 隐藏空提示
      if (emptyHint) {
        emptyHint.remove()
      }

      // 创建新留言卡片
      const messageCard = document.createElement('div')
      messageCard.className = 'message-card'

      // 获取当前时间
      const now = new Date()
      const dateStr = `${now.getFullYear()}-${(now.getMonth() + 1).toString().padStart(2, '0')}-${now.getDate().toString().padStart(2, '0')} ${now.getHours().toString().padStart(2, '0')}:${now.getMinutes().toString().padStart(2, '0')}`

      messageCard.innerHTML = `
        <div class="author">访客</div>
        <div class="content">${content}</div>
        <div class="time">${dateStr}</div>
      `

      // 添加到列表最前面
      messageList.insertBefore(messageCard, messageList.firstChild)

      // 清空输入框
      textarea.value = ''
      wordCount.textContent = '0/200字'
      wordCount.style.display = 'none'

      // 重置输入框和按钮高度
      textarea.style.height = ''
      button.style.height = ''
    }

    // 点击发布按钮
    button.addEventListener('click', publishMessage)

    // 按Enter发布（Shift+Enter换行）
    textarea.addEventListener('keydown', function (e) {
      if (e.key === 'Enter' && !e.shiftKey) {
        e.preventDefault()
        publishMessage()
      }
    })
  </script>
</body>

</html>